<template>
  <div class="info-left">
    <div v-for="(item, index) in publicArr" :key="index" class="info-leftCon">
      <p class="info-release" @click="item.isShow = !item.isShow">
        <span>{{ item.name }}</span>
        <i v-show="item.isShow" class="el-icon-arrow-down"></i>
        <i v-show="!item.isShow" class="el-icon-arrow-right"></i>
      </p>
      <template v-if="item.paramsKey === 'tag'">
        <div v-if="item.isShow" class="custom-tags">
          <div
            v-for="(exposure, i) in item.singleData"
            v-show="i < tagCount"
            :key="i"
            class="custom-tags-item"
            :class="{
              active:
                exposureList.indexOf(
                  `${item.paramsKey} = ${exposure.sourceKey || exposure.key}`,
                ) > -1,
            }"
            @click="exposureClick(item, exposure)"
          >
            {{ `${exposure.key}（${exposure.docCount}）` }}
          </div>
          <p
            v-if="item.singleData.length > tagCount"
            class="more"
            @click.stop="tagCount += 10"
          >
            更多
          </p>
        </div>
      </template>
      <template v-else>
        <div v-if="item.isShow" class="tags">
          <p
            v-for="(exposure, i) in item.singleData"
            v-show="!item.isMoreShow ? i < count : item.singleData"
            :key="i"
            class="info-leftText"
            :class="{
              active:
                exposureList.indexOf(
                  `${item.paramsKey} = ${exposure.sourceKey || exposure.key}`,
                ) > -1,
            }"
            @click="exposureClick(item, exposure)"
          >
            <template v-if="setIcon(item)">
              <img
                :src="iconPrefix(exposure.key)"
                alt=""
                loading="lazy"
                @error="setDefaultImage"
              />
            </template>
            <template v-else>
              <span :title="exposure.key" class="publicKye">
                {{ exposure.key }}</span
              >
            </template>
            <span>{{ exposure.docCount }}</span>
          </p>
          <p
            v-if="!item.isMoreShow && item.singleData.length > count"
            class="more"
            @click.stop="count += 10"
          >
            更多
          </p>
        </div>
      </template>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { defineProps, defineEmits, ref, watch } from 'vue';
import { useStore } from 'vuex';
const store = useStore();
const emit = defineEmits(['exposureHandle']);
const props = defineProps({
  publicData: {
    type: Object,
    default: () => {
      return {};
    },
  },
  category: {
    type: String,
    default: '',
  },
  flag: {
    type: Number,
    default: 0,
  },
});
const publicArr = ref<any>([]);
const leftList = ref<any>([]);
const exposureList = ref<any>([]);
const count = ref<any>(5);
const tagCount = ref<any>(10);
const twentyCount = ref<any>(20);
watch(()=>props.publicData,(newVal)=>{
    //  totalData = newVal
        const keys = Object.keys(newVal)
        const arr:any = []
        const allData = publicCon(newVal, props.category)
        for (let j = 0; j < allData.length; j++) {
          const obj = {
            ...allData[j],
            singleData: allData[j].singleData || [],
            isShow: true,
            isMoreShow: false,
          }
          arr.push(obj)
        }
        publicArr.value = arr
        tagCount.value = 10
})


const publicCon = (newVal, type) => {
  // 通用-start
  const common = [
    {
      key: 'tagAggregation',
      name: '标签',
      paramsKey: 'tag',
      singleData: newVal.tagAggregation,
    },
  ];

  const dataObj = {
    assetVul: [
      {
        key: 'vulLevelAggregation',
        name: '风险等级',
        paramsKey: 'vul_level',
        singleData: setLevel(newVal.vulLevelAggregation),
      },
      {
        key: 'vulTypeAggregation',
        name: '漏洞类型',
        paramsKey: 'vul_type',
        singleData: newVal.vulTypeAggregation,
      },
    ],
  };
  return [...common, ...dataObj[type]];
};
const setLevel = (val) => {
  let newArr: any = [];
  let newKey = '';
  val.forEach((el) => {
    if (el.key == 0) {
      newKey = '信息';
    }
    if (el.key == 1) {
      newKey = '低危';
    }
    if (el.key == 2) {
      newKey = '中危';
    }
    if (el.key == 3) {
      newKey = '高危 ';
    }
    if (el.key == 4) {
      newKey = '超危';
    }
    newArr.push({
      ...el,
      key: newKey,
    });
  });
  return newArr;
};
const iconPrefix = (url) => {
  let newUrl = '';
  if (url) {
    const substring = url.substring(4);
    newUrl = `/${substring}`;
  }
  return newUrl;
};
const setDefaultImage = (e) => {
  e.target.src = require('@/static/images/retrieval/full.png')
};
const setIcon = (item) => {
  const showList = [
    'webIconAggregation',
    'operatorsIconAggregation',
    'domainIspIconAggregation',
    'dataSourceIconAggregation',
    'iconAggregation',
  ];
  return showList.includes(item.key);
};
const clearClick = () => {
  leftList.value = [];
  exposureList.value = [];
  count.value = 5;
  twentyCount.value = 20;
};
const exposureClick = (item, res) => {
  const totalKey = `${item.paramsKey}=${res.sourceKey || res.key}`;
  const arrIndex = exposureList.value.indexOf(totalKey);
  if (arrIndex > -1) {
    // 取消选中
    exposureList.value.splice(arrIndex, 1);
  } else {
    // 点击选中
    exposureList.value.push(totalKey);
  }

  // 获取当前被选中的类型
  let strVal = '';
  const commonTags = store.state.attack.commonField;
  if (commonTags.includes(item.paramsKey)) {
    strVal = `(${item.paramsKey}=="${res.sourceKey || res.key}")`;
  } else {
    strVal = `(${props.category}.${item.paramsKey}=="${
      res.sourceKey || res.key
    }")`;
  }
  emit('exposureHandle', strVal, arrIndex > -1);
};

</script>
<style lang="scss" scoped>
.info-left {
  .info-leftCon {
    margin: 0px 12px;
    border-bottom: 1px solid $-v-color-border-light;
    padding: 10px 0;
    cursor: pointer;
    .info-release {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 14px;
      margin-left: 0;
      // color: #c0c8e1;
      font-weight: 600;
      // line-height: 30px;
      margin-bottom: 10px;
    }
    .info-leftText {
      display: flex;
      justify-content: space-between;
      align-items: center;
      font-size: 12px;
      font-weight: 500;
      line-height: 25px;
      letter-spacing: -0.35px;
      // color: #8590b3;
      line-height: 30px;
      .publicKye {
        display: inline-block;
        width: 85%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      img {
        width: 24px;
        height: 24px;
        border-radius: 4px;
      }
    }
    .info-leftText:hover {
      color: $-v-color-primary;
    }
    .more {
      font-family: SourceHanSansCN-Regular;
      font-size: 12px;
      font-weight: normal;
      line-height: 20px;
      letter-spacing: -0.35px;
      color: $-v-color-primary;
    }

    .custom-tags {
      display: flex;
      flex-wrap: wrap;
      &-item {
        padding: 0 0 0 8px;
        margin-right: 4px;
        margin: 5px 0;
        margin-right: 5px;
        min-height: 25px;
        font-family: PingFang SC;
        font-size: 12px;
        font-weight: normal;
        letter-spacing: 0em;
        line-height: 23px;
        // color: #8590b3;
        // background: rgba(210, 222, 235, 0.1);
        border: 1px solid rgba(210, 222, 235, 0.5);
        border-radius: 4px;
        word-break: break-all;
      }
      &-item:hover {
        color: $-v-color-primary;
        border: 1px solid $-v-color-primary;
        background: rgba(58, 125, 255, 0.1);
      }
      .more {
        margin-top: 10px;
        flex: 0 0 100%;
      }
    }
  }
}
</style>
